package com.gscitysfy.cus.drain.modules.duty.service.impl;

import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gscitysfy.cloudfrmwk.portal.feign.DictionaryService;

import com.gscitysfy.cus.drain.constants.DicConstant;
import com.gscitysfy.cus.drain.modules.duty.entity.MoperDutyMnChange;
import com.gscitysfy.cus.drain.modules.duty.mapper.MoperDutyMnChangeMapper;
import com.gscitysfy.cus.drain.modules.duty.service.IMoperDutyMnChangeService;
import com.gscitysfy.cus.drain.modules.duty.vo.MoperDutyMnChangeVo;
import com.gscitysfy.cus.drain.utils.HttpResponseUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 葛守跃
 * @version 1.0.0
 * @ClassName MoperDutyMnChangeServiceImpl
 * @Description 实现类
 * @creatTime 2022-03-29 16:47:17
 */
@Service
@Slf4j
public class MoperDutyMnChangeServiceImpl extends ServiceImpl<MoperDutyMnChangeMapper, MoperDutyMnChange> implements IMoperDutyMnChangeService {

    @Resource
    private MoperDutyMnChangeMapper changeMapper;

    @Resource
    private DictionaryService dictionaryService;

    /**
     * 分页获取交接班记录
     * @param changeVo
     * @return
     */
    @Override
    public IPage<MoperDutyMnChange> getChangeRecordByPage(Page<MoperDutyMnChange> page, MoperDutyMnChangeVo changeVo) {
        return changeMapper.getChangeRecordByPage(page, changeVo);
    }

    /**
     * 导出已归档的交接班记录
     * @param changeVo
     */
    @Override
    public void exportChangeRecord(MoperDutyMnChangeVo changeVo, HttpServletResponse response) {
        try(ExcelWriter writer = ExcelUtil.getWriter(true); OutputStream out = response.getOutputStream()) {
            String filename = "交接班归档记录.xlsx";

            HttpResponseUtil.setResponseHeaders(response, filename);
            writer.setOnlyAlias(true);
            //设置表头
            writer.addHeaderAlias("coamDutyDate", "值班日期");
            writer.addHeaderAlias("coamDutyType", "人员类别");
            writer.addHeaderAlias("coamClassessName", "班次");
            writer.addHeaderAlias("coamMonitorObjectId", "值守情况说明");
            writer.addHeaderAlias("coamContractId", "建议与意见");

            List<MoperDutyMnChange> changeRecord = changeMapper.getChangeRecord(changeVo);
            //判空，单独写入头数据
            if(changeRecord.isEmpty()){
                writer.writeHeadRow(new ArrayList<String>(){{
                    add("值班日期");add("人员类别");add("班次");add("值守情况说明");add("建议与意见");
                }});
            }else{
                //值班人员类型
                Map<String, String> dutyTypeMap = dictionaryService.getDictionaryMap(DicConstant.DUTY_TYPE).getData();
                changeRecord = changeRecord.stream().peek(
                        item -> item.setCoamDutyType(dutyTypeMap.get(item.getCoamDutyType()))
                ).collect(Collectors.toList());
            }
            writer.write(changeRecord, true);
            writer.flush(out);
        } catch (IOException e) {
            log.error("导出值班交接记录失败!");
        }
    }

}
